#include "cvariant.h"
#include <stdio.h>
#include <string.h>

// 自定义类型定义（见上文）
typedef struct { int x; int y; char name[32]; } Point;
static void point_copy(const void* src, void* dest);
static void point_destroy(void* data);

int main() {
    // 1. 注册自定义类型（获取类型ID）
    TypeId point_type = cvariant_register_custom_type(
        sizeof(Point),
        point_copy,
        point_destroy
    );
    printf("注册自定义类型 Point，ID: %d\n", point_type);

    // 2. 创建变体对象
    struct CVariant* var = cvariant_create();
    if (!var) {
        printf("创建变体失败\n");
        return 1;
    }

    // 3. 测试内置类型
    cvariant_set_int(var, 123);
    printf("类型: %d (预期0), 值: %d\n", cvariant_get_type(var), cvariant_get_int(var));

    cvariant_set_double(var, 3.1415926535);
    printf("类型: %d (预期2), 值: %lf\n", cvariant_get_type(var), cvariant_get_double(var));

    cvariant_set_str(var, "测试字符串");
    printf("类型: %d (预期3), 值: %s\n", cvariant_get_type(var), cvariant_get_str(var));

    // 4. 测试自定义类型（Point）
    Point p = {10, 20, "原点"};
    cvariant_set_custom(var, point_type, &p);
    
    if (cvariant_get_type(var) == point_type) {
        Point p_out;
        cvariant_get_custom(var, &p_out);
        printf("类型: %d (预期自定义ID), 值: (%d, %d, %s)\n",
               cvariant_get_type(var), p_out.x, p_out.y, p_out.name);
    }

    // 5. 清理
    cvariant_destroy(var);
    return 0;
}

// 实现自定义类型的复制和销毁函数
static void point_copy(const void* src, void* dest) {
    const Point* s = (const Point*)src;
    Point* d = (Point*)dest;
    d->x = s->x;
    d->y = s->y;
    strncpy(d->name, s->name, sizeof(d->name)-1);
}

static void point_destroy(void* data) {
    // 示例：无动态内存需要释放
}